package com.hebang.stcommunityapp.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;


import com.hebang.stcommunityapp.R;
import com.hebang.stcommunityapp.databinding.ActivityMainBinding;
import com.hebang.stcommunityapp.fragments.SyFragment1;
import com.hebang.stcommunityapp.fragments.SyFragment2;
import com.hebang.stcommunityapp.viewModel.MainViewModel;

import qiu.niorgai.StatusBarCompat;

public class MainActivity extends AppCompatActivity {
    private SyFragment1 syFragment1;
    private SyFragment2 syFragment2;
    FragmentManager fm;
    FragmentTransaction transaction;
    MainViewModel mainViewModel;
    ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setPagechengelisen(new PageChengelisenner());
        mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        binding.setMainVM(mainViewModel);
        binding.setLifecycleOwner(this);
        fm = getSupportFragmentManager();
        mainViewModel.getPageNum().setValue(1);
        setPage1();
    }




    public class PageChengelisenner {
        public void onpage(View view) {
            switch (view.getId()) {
                case R.id.tab_shouye:
                    mainViewModel.getPageNum().setValue(1);
                    setPage1();

                    break;
                case R.id.tab_wode:
                    mainViewModel.getPageNum().setValue(2);
                    setPage2();
                    Toast.makeText(MainActivity.this,"本页面功能尚未实现，敬请期待",Toast.LENGTH_LONG).show();

                    break;

            }
        }
    }

    private void setPage1() {
        StatusBarCompat.setStatusBarColor( this, 0xffffffff);
        transaction = fm.beginTransaction();
        transaction.disallowAddToBackStack();
        if (syFragment1 == null) {
            syFragment1 = new SyFragment1();
            transaction.add(R.id.tab_framelayout, syFragment1);
        }
        if (syFragment2 != null) transaction.hide(syFragment2);
        transaction.show(syFragment1);
        transaction.commit();
    }

    private void setPage2() {
        StatusBarCompat.setStatusBarColor( this, 0xff286DEF);
        transaction = fm.beginTransaction();
        transaction.disallowAddToBackStack();
        if (syFragment2 == null) {
            syFragment2 = new SyFragment2();
            transaction.add(R.id.tab_framelayout, syFragment2);
        }
        if (syFragment1 != null) transaction.hide(syFragment1);
        transaction.show(syFragment2);
        transaction.commit();
    }
}